package me.xiaoyan.android.net;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import me.xiaoyan.android.net.HttpManager.HTTPMETHOD;



public class AsyncHttpGet {
	private static ExecutorService executorService = Executors
			.newFixedThreadPool(5); // 固定五个线程来执行任务

	/**
	 * 获取 网络数据 
	 * @param url
	 * @param params
	 * @param httpMethod
	 * @param listener
	 */
	public static void request(final String url, final WebParameters params,
			final HTTPMETHOD httpMethod, final RequestListener listener) {
		executorService.submit(new Runnable() {
			
			@Override
			public void run() {
				try {
					Object resp = HttpManager.openUrl(url, httpMethod, params,null).getContent();
					listener.onComplete(resp);
				} catch (Exception e) {
					listener.onError(e);
				}
			}
		});
	}
	
	/**
	 * 获取 网络数据 
	 * @param url
	 * @param params
	 * @param httpMethod
	 * @param listener
	 */
	public static void request(final String url, final String params,
			final HTTPMETHOD httpMethod, final RequestListener listener) {
		executorService.submit(new Runnable() {
			
			@Override
			public void run() {
				try {
					Object resp = HttpManager.openUrl(url, httpMethod, params);
					listener.onComplete(resp);
				} catch (Exception e) {
					listener.onError(e);
				}
			}
		});
	}
}
